<?php
namespace app\houtai\controller\user;
use app\houtai\controller\BaseController;

class SignController extends BaseController {
  /**
   *  @brief  : 用户管理-签到记录查询
   *  @return : page
   *  @details: 
   */
  public function indexAction() {
    $conds = [];
    $order = [];
    $LOGIN_TYPE = config('LOGIN_TYPE');
    $CHANNELS   = $this->GetAdminChannel();
    //////////////////////////////////////////////////////////////////////////////////////
    $Channel = $this->request->param('Channel/d', -1);
    if ($Channel > 0 && array_key_exists($Channel, $CHANNELS)) {
      $conds['b.Channel'][] = ['IN', array_keys($CHANNELS)];
      $conds['b.Channel'][] = ['EQ', $Channel];
    } else {
      $conds['b.Channel'] = ['IN', array_keys($CHANNELS)];
    }
    ////////////////////////////
    $time1 = $this->request->param('time1/s', '', 'trim');
    $time2 = $this->request->param('time2/s', '', 'trim');
    
    $aa = YmdHis2Unix($time1);
    $bb = YmdHis2Unix($time2);
    $ta = date('Y-m-d 00:00:00', $aa);
    $tb = date('Y-m-d 23:59:59', $bb);
    if ($aa > 0) {
      if ($bb > 0) {
        if ($aa <= $bb) {
          $conds['a.CollectDate'] = ['BETWEEN TIME', [$ta, $tb]];
        } else {
          $conds['a.CollectDate'] = ['>= TIME', $ta];
        }
      } else {
        $conds['a.CollectDate'] = ['>= TIME', $ta];
      }
    } else {
      if ($bb > 0) {
        $conds['a.CollectDate'] = ['<= TIME', $tb];
      }
    }
    ////////////////////////////
    $search = $this->request->param('search/s', '', 'trim');
    if (!empty($search)) {
      if (is_numeric($search)) {
        $search = $this->request->param('search/d', 0);
        if ($search > 0) {
          $conds['b.UserID|b.GameID'] = $search;
        }
      } else {
        $conds['b.NickName'] = ['LIKE', "%{$search}%"];
      }
    }
    //////////////////////////////////////////////////////////////////////////////////////
    $order_map = [
      'A' => 'RecordID',
      'B' => 'Channel',
      'C' => 'GameID',
      'D' => 'Gold',
      'E' => 'ClinetIP',
      'F' => 'CollectDate',
    ];
    $this->orderField = array_key_exists($this->orderField, $order_map) ? $this->orderField : 'A';
    $order[$order_map[$this->orderField]] = $this->orderDirection;
    //////////////////////////////////////////////////////////////////////////////////////
    $list = table('CYRecordDB.dbo.RecordSignin a', 'rydb.record')->field([
      'a.RecordID', 'a.UserID', 'a.Gold', 'a.ClinetIP', 'a.CollectDate', 'b.GameID',
      'b.NickName', 'b.AccountsType', 'b.Channel', "ISNULL(b.AccountImgUrl, '')" => 'Avatar',
      'b.Accounts', 'b.Compellation', 'b.PassPortID', 'b.RegisterMobile', 'b.Sex',
      'b.RegisterDate', 'b.RegisterIP', 'b.GameLogonCount', 'b.LastLogonDate',
      'b.LastLogonIP'
    ])
    ->join('CYBasicDB.dbo.AccountsInfo b',   'b.UserID = a.UserID', 'LEFT')
    ->where($conds)->order($order)
    ->paginate();
    
    //////////////////////////////////////////////////////////////////////////////////////
    return $this->fetch('', [
      'list'  => $list,
      'search'=> $search,
      'time1' => $time1,
      'time2' => $time2,
      'Channel'   => $Channel,
      'CHANNELS'  => $CHANNELS,
    ]);
  }
}
